package com.zzh.myapplication.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * Created by Administrator on 2016/3/22.
 */
public class OrderListDetailActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{
    private Toolbar toolbar;
    private TextView search_voice;
    private TextView orderNum;
    private TextView orderCarNum;
    private TextView addrass;
    private TextView car_rental_time;
    private TextView car_rental_time_n;
    private TextView return_car_time;
    private TextView return_car_time_m;
    private TextView addrass_car_rental;
    private TextView addrass_return_car;
    private TextView car_rental_money;
    private TextView rental_money;
    private TextView tv_rental_money;
    private TextView tv_li_money;
    private TextView orderStatus;

    private TextView total_money;
    private TextView car_unitPrice;
    private TextView car_totalAmt;
    private TextView orderCarName;
    public SimpleDraweeView carPic;
    public String ordernum;
    public String ordercarNum;
    private LoadingUtil loadingUtil;
    public Button certificate_submit_btn;
    String order;

    private RadioGroup mRadioGroup;

    private RadioButton rbSubimt;


    @Override
    public void setContent() {
        setContentView(R.layout.activity_order_list_detail);
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        search_voice = (TextView) findViewById(R.id.search_voice);
        carPic = (SimpleDraweeView) findViewById(R.id.carPic);
        orderNum = (TextView) findViewById(R.id.orderNum);
        certificate_submit_btn = (Button) findViewById(R.id.certificate_submit_btn);
        orderCarNum = (TextView) findViewById(R.id.orderCarNum);
        addrass = (TextView) findViewById(R.id.addrass);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.order_detail_pay));
        car_rental_time = (TextView) findViewById(R.id.car_rental_time);
        car_rental_time_n = (TextView) findViewById(R.id.car_rental_time_n);
        orderStatus = (TextView) findViewById(R.id.orderStatus);
        orderCarName = (TextView) findViewById(R.id.orderCarName);
        car_totalAmt = (TextView) findViewById(R.id.car_totalAmt);
        car_unitPrice = (TextView) findViewById(R.id.car_unitPrice);
        tv_rental_money = (TextView) findViewById(R.id.tv_rental_money);
        tv_li_money = (TextView) findViewById(R.id.tv_li_money);
        total_money = (TextView) findViewById(R.id.total_money);
        mRadioGroup = (RadioGroup)findViewById(R.id.tab_select);
        rbSubimt = (RadioButton)findViewById(R.id.rb_submit_now);

        loadingUtil = new LoadingUtil(this,getString(R.string.loading_text));

        String time = getIntent().getStringExtra("applyTime");
        String[] s = time.split("\\ ");
        car_rental_time.setText(s[1]);
        car_rental_time_n.setText(s[0]);
        order = getIntent().getStringExtra("orderStatus");
        if (order.equals("已结算")||order.equals("关闭")) {
            /*search_voice.setText("取消");*/
        } else {
            mRadioGroup.setVisibility(View.VISIBLE);
        }

        orderStatus.setText(order);
    }

    @Override
    public void setListener() {
        search_voice.setOnClickListener(this);
        certificate_submit_btn.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onAfterInit() {
        loadingUtil = new LoadingUtil(OrderListDetailActivity.this, "查询中...");
        Intent intent = getIntent();
        ordernum = intent.getStringExtra("setOrderNum");
        ordercarNum = intent.getStringExtra("orderCarNum");
        orderNum.setText(ordernum);
        orderCarNum.setText(ordercarNum);
        SetOrderListsJson();

        rbSubimt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("money", money +"");
                intent.putExtra("ordernum", ordernum);
                intent.putExtra("ordercarNum", ordercarNum);
                intent.setClass(OrderListDetailActivity.this, OrderPayCenterActivity.class);
                start_Activity(intent);
            }
        });

    }

    public void SetOrderListsJson() {
        JSONObject json = new JSONObject();
        try {
            json.put("appNo", ordernum);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.get().url(UrlValues.GET_ORDER_DETAIL).addParams("input", json.toString()).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                getOrderDetailRespponse(response);
                System.out.print("order－－－" + response + "\n");
            }
        });
    }

    String money_zu;
    String money_bao;
    int money;

    public void getOrderDetailRespponse(String date) {
        try {
            JSONObject result = new JSONObject(date.toString());
            String autoModelName = result.optString("autoModelName");
            orderCarName.setText(autoModelName);
            /*String sp = result.getString("pttrcTime");
            String[] s = sp.split("\\ ");
            car_rental_time.setText(s[1]);
            car_rental_time_n.setText(s[0]);*/
            JSONArray prcChargeDetList = result.getJSONArray("prcChargeDetList");
            for (int i = 0; i < prcChargeDetList.length(); i++) {
                JSONObject object = prcChargeDetList.getJSONObject(i);
                if (object.optString("itemName").equals("租金")) {
                    car_totalAmt.setText(object.optString("totalAmt"));
                    car_unitPrice.setText(object.optString("unitPrice"));
                    money_zu = object.optString("pricingAmt");
                    tv_rental_money.setText(object.optString("pricingAmt"));
                }
                if (object.optString("itemName").equals("保险费")) {
                    money_bao = object.optString("pricingAmt");
                    tv_li_money.setText(object.optString("pricingAmt"));
                }
            }
            money = Integer.parseInt(money_bao) + Integer.parseInt(money_zu);
            total_money.setText(money + "");
            JSONArray array = result.getJSONArray("carImgList");
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsons = array.getJSONObject(i);
                carPic.setImageURI(Uri.parse(UrlValues.CAR_IMG_ID + jsons.getString("imgId")));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.search_voice:
                    intent.setClass(OrderListDetailActivity.this, OrderListItmeEvaluateActivity.class);
                    start_Activity(intent);
                    finish();
                break;
            case R.id.certificate_submit_btn:
                intent.putExtra("money", money + "");
                intent.putExtra("ordernum", ordernum);
                intent.putExtra("ordercarNum", ordercarNum);
                intent.setClass(OrderListDetailActivity.this, OrderPayCenterActivity.class);
                start_Activity(intent);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkID) {
        switch (checkID) {
            case R.id.rb_cancel_order:
                String userPhone = SPUtils.getString(this,"LoginInfo",null);
                JSONObject jsons = new JSONObject();
                try {
                    jsons.put("appNo",ordernum);
                    jsons.put("mobile",userPhone);
                    loadingUtil.startShowLoading();
                    PublicRequest.doGetRequest(UrlValues.CANCEL_RENT_CAR_ORDER_URL, handler, jsons,10000,30000);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                break;
//            case R.id.rb_submit_now:
//
//                break;
            default:
                break;
        }

    }

    protected Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.handleMessage(msg);
            loadingUtil.stopShowLoading();
            switch (msg.what) {
                case 10000:
//                    Intent intent = new Intent(OrderListDetailActivity.this,);
                    finish();
                    T.showShort("取消成功");
                    break;
                case 30000:
                    break;
                default:
                    break;
            }
        }
    };
}
